<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="./css/aui.css" />    
    <link rel="stylesheet" type="text/css" href="./css/aui-slide.css" />
    <link rel="stylesheet" type="text/css" href="./css/font-awesome.css" />
    <link rel="stylesheet" type="text/css" href="./css/style.css" />
    <script type="text/javascript" src="./script/jquery.min.js"></script>
    <script type="text/javascript" src="./script/doT.min.js"></script>
    <script type="text/javascript" src="./script/base.js"></script>
    <script type="text/javascript" src="./script/api.js"></script></head>
    <script type="text/javascript" src="./script/jquery-3.2.0.js"></script>
</head>
<body>
    <header class="header header-f" style="padding-top:2rem; height:4rem;">
        <div class="header-l aui-pull-left" onclick="closeWin()">
            <i class="fa fa-angle-left"></i>
        </div>
        <div class="header-title">
            编辑
        </div>
        <div class="header-r aui-pull-right">
        </div>
    </header>
    <div style="height:4rem;"></div>

    <div class="aui-content aui-margin-b-15">
        <ul class="aui-list aui-list-in">
            <div id="list">
<!--             <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">
                        姓名：
                    </div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请填写收货人姓名">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">
                        手机号：
                    </div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请填写联系电话">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">
                        所在地区：
                    </div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="所在地区">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">
                        详细地址：
                    </div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="街道，楼牌号等">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">
                        设置默认地址
                    </div>
                    <div class="aui-list-item-input">
                        <input type="checkbox" class="aui-switch aui-pull-right" checked>
                    </div>
                </div>
            </li> -->
        </div>
    </ul>
</div>

<div class="aui-content-padded">
    <div class="aui-btn aui-btn-danger aui-btn-block aui-btn-sm" onclick="saveAddress();">保存地址</div>
</div>


</body>
<script type="text/javascript" src="./script/api.js"></script>
<script type="text/template" id="template">
    <li class="aui-list-item">
        <div class="aui-list-item-inner">
            <div class="aui-list-item-label">
                姓名：
            </div>
            <div class="aui-list-item-input">
                <input type="text" value="{{=it.name}}" id="name">
            </div>
        </div>
    </li>
    <li class="aui-list-item">
        <div class="aui-list-item-inner">
            <div class="aui-list-item-label">
                手机号：
            </div>
            <div class="aui-list-item-input">
                <input type="text" value="{{=it.tel}}" id="tel" maxlength="11">
            </div>
        </div>
    </li>
    <li class="aui-list-item">
        <div class="aui-list-item-inner" id="addressView">
            <div class="aui-list-item-label">
                所在地区：
            </div>
            <div class="aui-list-item-input" id="city">北京市 北京市 东城区
                <!-- <input type="text" placeholder="所在地区"> -->
            </div>
        </div>
    </li>
    <li class="aui-list-item">
        <div class="aui-list-item-inner">
            <div class="aui-list-item-label">
                详细地址：
            </div>
            <div class="aui-list-item-input">
                <input type="text" value="{{=it.address}}" id="address">
            </div>
        </div>
    </li>
    {{? it.default != 1}}
    <li class="aui-list-item">
        <div class="aui-list-item-inner">
            <div class="aui-list-item-label">
                设置默认地址
            </div>
            <div class="aui-list-item-input">
                <input type="checkbox" id="default" onclick="changeChecked();" class="aui-switch aui-pull-right" 

                >
            </div>
        </div>
    </li>
    {{?}}
</script>
<script type="text/javascript">

    apiready = function(){
        proName = null;
        cityName = null;
        dirName = null;
        add_id = api.pageParam.id;
        loadData(add_id);
    }

    function addChooseAddress(){
        var addressView = api.require('addressView');
        addressView.open({
        file_addr: 'widget://res/district.txt', //数据源地址
        selected_color: '#ff0000', //颜色
        pro_id: 110000, //省id
        city_id: 110100, //市id
        dir_id: 110101 //区id
    });
        $api.addEvt($api.byId("addressView"), 'click', function() {
            addressView.show({}, function(ret, err) {
                if (ret.status) {
                // alert(JSON.stringify(ret)); //返回值
                var data = ret.data;
                proName = data[0].name;
                cityName = data[1].name;
                dirName = data[2].name;
                var address = proName + ' ' + cityName + ' ' + dirName;
                $('#city').html(address);
            }
        });
        });
    }

    function loadData(id){
        var userid = $api.getStorage('userid');
        var authtoken = $api.getStorage('authtoken');
        api.ajax({
            url: webAddress + '/a/order/address/info',
            method: 'get',
            data: {
                values: {
                    id: id,
                },
            },
            headers: {
                TOKEN: token,
                authuid: userid,
                authtoken: authtoken,
            },
        },function(ret, err){
            if (ret) {
                // alert( JSON.stringify( ret ) );
                if (ret.result) {

                    // 判断是否为默认地址 存全局变量
                    if (ret.result.default == 1) {
                        defaultValue = 1;
                    }else{
                        defaultValue = 0;
                    }
                    templateFun(ret.result);
                }
                addChooseAddress();

            } else {
                // alert( JSON.stringify( err ) );
                api.toast({
                    msg: err.body.message,
                    duration: 2000,
                    location: 'bottom'
                });
                return false;
            }
        });
    }

    function saveAddress(){
        // var isDefault = $('#default').attr("checked");
        // if (isDefault) {
        //     defaultValue = 1;
        // }else{
        //     defaultValue = 0;
        // }

        var name = $api.byId('name').value;
        var tel = $api.byId('tel').value;
        var address = $api.byId('address').value;

        var userid = $api.getStorage('userid');
        var authtoken = $api.getStorage('authtoken');

        if (!proName) {
            api.toast({
                msg: '请选择省市区!',
                duration: 2000,
                location: 'bottom'
            });
            return false;
        }

        api.ajax({
            url: webAddress + '/a/order/Address/edit',
            method: 'post',
            data: {
                values: {
                    name: name,
                    tel: tel,
                    province: proName,
                    city: cityName,
                    region: dirName,
                    address: address,
                    default: defaultValue,
                    add_id: add_id,
                },
            },
            headers: {
                TOKEN: token,
                authuid: userid,
                authtoken: authtoken,
            },
        },function(ret, err){
            if (ret) {
                // alert( JSON.stringify( ret ) );
                api.toast({
                    msg: ret.message,
                    duration: 2000,
                    location: 'bottom',
                    global: 'true',
                });
                api.sendEvent({
                    name: 'reload'
                });
                api.closeWin();
            } else {
                // alert( JSON.stringify( err ) );
                api.toast({
                    msg: err.body.message,
                    duration: 2000,
                    location: 'bottom'
                });
                return false;
            }
        });
    }

    // 模板赋值函数
    function templateFun(data){
        if (data) {
          var template = $api.byId('template');
          var dotFn = doT.template(template.innerHTML);
          var html = dotFn(data);
          var list = $api.byId('list');
          $api.html(list, html);
      }
  }

    // 改变状态(是否为默认地址)
    function changeChecked(){
        var isDefault = $('#default').attr("checked");

        if (isDefault) {
            $('#default').attr('checked',null);
            defaultValue = 0;
        }else{
            $('#default').attr('checked','checked');
            defaultValue = 1;
        }
    }

    function closeWin(){
        api.closeWin({
        });
    }
</script>
</html>